package com.enqualcomm.kids.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.enqualcomm.kids.BuildConfig;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.base.MyResultReceiver;
import com.enqualcomm.kids.component.InitContextIntentService;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.mvp.login.wx.presenter.LoginPresenter;
import com.enqualcomm.kids.mvp.login.wx.presenter.Presenter;
import com.enqualcomm.kids.mvp.login.wx.view.WXLoginView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import common.utils.MyLogger;
import common.utils.PlatformUtil;
import common.utils.PromptUtil;

/**
 * Created by Sunzhigang on 2016/9/7.
 */

@EActivity(R.layout.activity_choose_login)
public class ChooseLoginActivity extends BaseActivity implements WXLoginView {

    Presenter presenter;
    @ViewById(R.id.choose_longin_logo_iv)
    ImageView choose_longin_logo_iv;
    @ViewById(R.id.choose_longin_logo_ll)
    View choose_longin_logo_ll;

    @AfterViews
    void setUpView() {
        presenter = new LoginPresenter(this, this);
        if ("西瓜皮-西瓜皮".equals(BuildConfig.CHANNEL)) {
            choose_longin_logo_iv.setVisibility(View.VISIBLE);
            choose_longin_logo_ll.setVisibility(View.GONE);
        } else {
            choose_longin_logo_iv.setVisibility(View.GONE);
            choose_longin_logo_ll.setVisibility(View.VISIBLE);
        }
    }

    @Click(R.id.wx_login_rl)
    void wxLogin() {
        presenter.start();
    }

    @Click(R.id.phone_login_rl)
    void phoneLoin() {
        startActivity(LoginActivity.class);
        finish();
    }


    @Override
    public void startLogin() {
        showProgress();
    }

    @Override
    public void successLogin(boolean isSuccess) {
        hideProgress();
        //TODO 根据返回值1-成功已绑定手机号或者是已经有手表，2-新用户需要绑定账号或者是绑定手机号
        if (isSuccess) {
            showProgress();
            new AppDefault().setLoginByWX(true);
            Intent intent = new Intent(this, InitContextIntentService.class);
            intent.putExtra(InitContextIntentService.RECEIVER, new MyResultReceiver(new Handler(), this));
            startService(intent);
        } else {
            MyLogger.kLog().i("=====绑定账号");
            startActivity(BindPhoneActivity_.class);
        }
    }

    @Override
    public void waitLongTime() {
        hideProgress();
        PromptUtil.toast(this, "登录失败,请再次进行登录");
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        hideProgress();
        MyLogger.kLog().i("resultCode=============" + resultCode);
        if (resultCode == InitContextIntentService.SUCCESS) {
            int windowWidth = PlatformUtil.windowWidth(this);
            int windowHeight = PlatformUtil.windowHeight(this);
            AppDefault appDefault = new AppDefault();
            appDefault.setWindowWidth(windowWidth);
            appDefault.setWindowHeight(windowHeight);
            startActivity(MainActivity_.class);
            finish();
        } else {
            PromptUtil.toast(getApplicationContext(), R.string.network_error);
        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long l = SystemClock.elapsedRealtime();
            if ((l - exitTime) > 2000) {
                PromptUtil.toast(getApplicationContext(), R.string.press_to_exit);
                exitTime = l;
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
